In this project, we’ll use an Myduino AIoT Education Kit with built-in an ultrasonic sensor, a buzzer, LEDs, and an I2C LCD to build a Smart Parking Lot Alert System. You’ll be able to detect vehicles approaching with proximity alerts and automatically monitor parking space availability. The system provides visual feedback through LEDs and displays real-time status on the LCD display.
Perfect for students, makers, or IoT beginners who want to explore distance sensing + automated parking management with ESP32. Let’s go from zero to hero!
Objective

In this project, you’ll learn how to:
- Detect proximity with an ultrasonic sensor and provide variable-speed buzzer alerts.
- Monitor parking space occupancy with intelligent timing-based detection.
- Display parking status and system information on an I2C LCD.
- Control LEDs for visual status indication (Green = Available, Red = Occupied).
- Understand how sensors, outputs, and microcontrollers work together in a smart parking system.
By the end, you’ll have your own Smart Parking Lot Alert System running on ESP32!
Circuit Connections

| Components | ESP32 Dev Module |
| Ultrasonic TRIG | IO2 |
| Ultrasonic ECHO | IO4 |
| LCD 16×2 SDA | SDA |
| LCD 16×2 SCL | SCL |
| Buzzer | IO5 |
| Green LED | IO27 |
| Red LED | IO26 |
Logic Flow
- The ultrasonic sensor measures distance to detect approaching objects.
- Proximity Alert System: When objects approach, the buzzer beeps at different speeds:
- Stage 1 (≤30cm): Slow beeping (1000ms intervals)
- Stage 2 (≤20cm): Medium beeping (500ms intervals)
- Stage 3 (≤10cm): Fast beeping (200ms intervals)
- Parking Detection: When an object stays between 11-20cm for +4 seconds, the system marks the spot as “Parking Occupied”.
- Status Display: LCD shows parking availability and LEDs provide visual feedback.
- Smart Control: Buzzer automatically turns OFF when a vehicle is parked.
Code Lab
Step 1: Install Library
Before uploading the code, you need to install this library in Arduino IDE:
- LiquidCrystal_I2C.h by Frank de Brabander (for I2C LCD)
- Wire.h (already included in ESP32, no need to download)
for example:

Step 2: Code
Copy and paste this code into Arduino IDE:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions
#define TRIG_PIN 2
#define ECHO_PIN 4
#define BUZZER_PIN 5
#define RED_LED_PIN 26
#define GREEN_LED_PIN 27
// I2C LCD setup (address 0x27, 16x2 display)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Distance thresholds for proximity detection (in cm)
#define STAGE_1_DISTANCE 30 // Far - slow beeping
#define STAGE_2_DISTANCE 20 // Medium - medium beeping
#define STAGE_3_DISTANCE 10 // Close - fast beeping
// Parking detection
#define PARKING_MIN_DISTANCE 11 // Minimum distance for parking detection (in cm)
#define PARKING_MAX_DISTANCE 20 // Maximum distance for parking detection (in cm)
#define PARKING_DELAY 4000 // 4 seconds delay before marking as occupied
// Variables
unsigned long lastBeepTime = 0;
unsigned long objectDetectedTime = 0;
bool isParked = false;
bool objectPresent = false;
int beepInterval = 0;
void setup() {
// Initialize pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
// Initialize I2C LCD
Wire.begin();
lcd.init();
lcd.backlight();
lcd.clear();
// Initialize serial for debugging
Serial.begin(9600);
// Initial state - parking available
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
lcd.setCursor(0, 0);
lcd.print("Smart Parking");
lcd.setCursor(0, 1);
lcd.print("Available");
Serial.println("Smart Parking System Initialized");
}
void loop() {
// Get distance from ultrasonic sensor
long distance = getDistance();
// Handle proximity detection and buzzer beeping
handleProximityAlert(distance);
// Handle parking detection
handleParkingDetection(distance);
delay(100); // Small delay for system stability
}
long getDistance() {
// Clear the trigger pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Send 10 microsecond pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin and calculate distance
long duration = pulseIn(ECHO_PIN, HIGH);
long distance = (duration * 0.034) / 2; // Convert to cm
return distance;
}
void handleProximityAlert(long distance) {
unsigned long currentTime = millis();
// Determine beep interval based on distance
if (distance <= STAGE_3_DISTANCE && distance > 0) {
beepInterval = 200; // Fast beeping - 200ms interval
} else if (distance <= STAGE_2_DISTANCE && distance > 0) {
beepInterval = 500; // Medium beeping - 500ms interval
} else if (distance <= STAGE_1_DISTANCE && distance > 0) {
beepInterval = 1000; // Slow beeping - 1000ms interval
} else {
beepInterval = 0; // No beeping
}
// Handle buzzer beeping only when not parked
if (beepInterval > 0 && !isParked) {
if (currentTime - lastBeepTime >= beepInterval) {
digitalWrite(BUZZER_PIN, HIGH);
delay(50); // Short beep duration
digitalWrite(BUZZER_PIN, LOW);
lastBeepTime = currentTime;
// Debug output
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm - Stage: ");
if (beepInterval == 200) Serial.println("3 (Close)");
else if (beepInterval == 500) Serial.println("2 (Medium)");
else if (beepInterval == 1000) Serial.println("1 (Far)");
}
}
}
void handleParkingDetection(long distance) {
unsigned long currentTime = millis();
// Check if object is in parking range (11-20cm)
if (distance >= PARKING_MIN_DISTANCE && distance <= PARKING_MAX_DISTANCE) {
if (!objectPresent) {
// Object just detected, start timer
objectPresent = true;
objectDetectedTime = currentTime;
Serial.println("Object detected in parking range");
} else {
// Object still present, check if enough time has passed
if (!isParked && (currentTime - objectDetectedTime >= PARKING_DELAY)) {
// Mark as parked
isParked = true;
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW); // Turn off buzzer
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parking Status:");
lcd.setCursor(0, 1);
lcd.print("OCCUPIED");
Serial.println("Parking spot occupied - Buzzer OFF");
}
}
} else {
// No object detected
if (objectPresent || isParked) {
// Object removed or was parked
objectPresent = false;
isParked = false;
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Parking Status:");
lcd.setCursor(0, 1);
lcd.print("AVAILABLE");
Serial.println("Parking spot available");
}
}
}
Step 3: Upload and Run
- Connect your ESP32 board to your computer
- Select the correct board and port in Arduino IDE
- Upload the code
- Open Serial Monitor at 9600
- Try moving objects near the ultrasonic (better use flat object).
System Check
When working properly:
- Proximity Detection: Buzzer beeps at different speeds as objects approach (30cm, 20cm, 10cm thresholds)
- Parking Detection: When object stays 11-20cm for 4+ seconds, red LED turns on and LCD shows “OCCUPIED”
- Status Display: LCD shows real-time parking status
- Smart Control: Buzzer stops when parking spot is occupied
- Visual Feedback: Green LED = Available, Red LED = Occupied
Troubleshooting Guide
| Problem | Solution |
| Ultrasonic sensor not detecting | Check TRIG pin connected to GPIO2 and ECHO pin to GPIO4 |
| LCD not displaying text | Confirm I2C connections SDA to SDA, SCL to SCL. Try address 0x3F if 0x27 doesn’t work |
| Buzzer not working | Check buzzer connected to GPIO5 and polarity |
| LEDs not lighting | Verify LED connections to GPIO16(Green) and GPIO17(Red), check current-limiting resistors |
| False parking detection | Adjust PARKING_MINIMUM_DISTANCE and PARKING_MAXIMUM_DISTANCE values |
| Buzzer too sensitive/not sensitive enough | Modify STAGE_1_DISTANCE, STAGE_2_DISTANCE, STAGE_3_DISTANCE values |
Tips: If you want to find any lines in Arduino IDE, click CTRL+F (CMD + F on Mac) and search the line you want to find (applicable to any platform).
Customizations Options
- Adjust Detection Ranges: Modify distance thresholds for different parking scenarios
- Change Timing: Adjust PARKING_DELAY for faster/slower parking confirmation
- Sound Patterns: Customize beep intervals and duration
- Display Messages: Modify LCD text for different languages or information
- Add Features: Include WiFi connectivity for remote monitoring
Perfect for: IoT learning projects, smart city demonstrations, automated parking systems, and sensor integration practice!
Buy from:
Myduino AIoT Education Kit from Myduino.com






